
/*--------------------------------------------------------------------------------------------
* Replication code for supplementary appendix. 

	User-written packages used: plotplain, grc1leg, frmttable, tost.
	
	Type ssc install [package_name] to install before running this code. For -tost-, 
	please run 'findit tost' and follow the prompts.
	
	Note: Data for Figure A1 are embargoed for use in another project.
	
*-------------------------------------------------------------------------------------------*/

clear *
set scheme plotplain

* set up conjoint data
cap prog drop setup_conjoint
prog def setup_conjoint
	use conjoint-exp-data, clear
	merge m:1 caseid using demographics-data
	drop _m
	merge m:1 caseid using info-exp-data
	drop _m
	merge m:1 caseid using raked-ps-weights
	drop _m
	replace raked_wt = weight if raked == . & weight ~= .
	merge 1:1 caseid id task profile using conjoint-weights-US
	drop if _m == 2
	drop _m
	ren raked_wtn weight_conjoint
	replace weight_conjoint = weight*weight_conjoint if weight_conjoint ~= .
	replace weight_conjoint = raked_wt if weight_conjoint == .
	drop raked_wt weight
	gen c_wealth_quint = 0 if c_wealth < 3
	replace c_wealth_quint = 1 if c_wealth == 3 | c_wealth == 4
	replace c_wealth_quint = 2 if c_wealth == 5 | c_wealth == 6
	replace c_wealth_quint = 3 if c_wealth == 7 | c_wealth == 8
	replace c_wealth_quint = 4 if c_wealth == 9
	gen assets_q = .
	forval i = 1/4 {
		egen assets_q_`i' = cut(assets) if country == `i', g(5)
		replace assets_q = assets_q_`i' if country == `i'
		drop assets_q_`i'
	}
end

* set up info experiment data
cap prog drop setup_info
prog def setup_info
	use info-exp-data, clear
	merge 1:1 caseid using demographics-data
	drop if _m == 2
	drop _m
	merge 1:1 caseid using raked-ps-weights
	drop _m
	replace raked_wt = weight if raked == . & weight ~= .
	drop weight
	replace income = income-1
	gen assets_q = .
	forval i = 1/4 {
		egen assets_q_`i' = cut(assets) if country == `i', g(5)
		replace assets_q = assets_q_`i' if country == `i'
		drop assets_q_`i'
	}
end

* set up US YouGov conjoint 
cap prog drop setup_conjoint_us
prog def setup_conjoint_us
	use us-conjoint-exp-data, clear
	gen c_wealth_quint = 0 if c_wealth < 3
	replace c_wealth_quint = 1 if c_wealth == 3 | c_wealth == 4
	replace c_wealth_quint = 2 if c_wealth == 5 | c_wealth == 6
	replace c_wealth_quint = 3 if c_wealth == 7 | c_wealth == 8
	replace c_wealth_quint = 4 if c_wealth == 9
end

* define categorical prior about legislators' wealth
cap prog drop dist
prog def dist
	gen dist_ave_q = 1 if dist_ave < -3.24
	replace dist_ave_q = 2 if dist_ave >= -3.24 & dist_ave < -1.45
	replace dist_ave_q = 3 if dist_ave >= -1.45 & dist_ave < -.15
	replace dist_ave_q = 4 if dist_ave >= -.15 & dist_ave ~= .
end

* prep of main wealth effects
cap prog drop mainamce
prog def mainamce
	mat tab = r(table)
	mat e = J(5,3,.)
	forval i = 1/5 {
		mat e[`i',1] = tab[1,`i']
		mat e[`i',2] = tab[5,`i']
		mat e[`i',3] = tab[6,`i']
	}
	clear
	svmat e
	gen n = _n
end

* prep for subgroup graphs
cap prog drop mfxgraph5
prog def mfxgraph5
	mat tab = r(table)
	mat e = J(10,3,.)
	forval i = 1/10 {
		mat e[`i',1] = tab[1,`i']
		mat e[`i',2] = tab[5,`i']
		mat e[`i',3] = tab[6,`i']
	}
	clear
	svmat e
	gen n = _n in 1/5
	replace n = _n-4.5 in 6/10
	gen g = 1 in 1/5
	replace g = 2 in 6/10
end

* prep for in-group bias graphs
cap prog drop matprep
prog def matprep
	mat tab = r(table)
	mat b = tab[1..1,1..4]'
	mat lb = tab[5..5,1..4]'
	mat ub = tab[6..6,1..4]'
	mat est = b,lb,ub
	clear
	svmat est
	gen n = _n
end

* dummy out demographics
cap prog drop tabout
prog def tabout
	foreach x in $tabout {
		tab `x', gen(`x')
	}
end

* conjoint graph
cap prog drop graph_amce 
program define graph_amce
	args y w /* y = outcome variable 
				w = x-axis label */
	reg `y' i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs ///
		$addcovs $cond $cond2 $wgt, cl(caseid)
	mat coef = e(b)
	mat var = vecdiag(e(V))
	matmap var se, m(sqrt(@))
	mat coef = coef', se'
	scalar R = rowsof(coef)-1
	mat coef = coef[1..R,1..2]
	clear
	svmat coef
	gen names = ""
	local names : rownames coef
	local k = 1
	foreach j of local names {
		replace names = "`j'" in `k'
		local k = `k' + 1
		}
	gen drop1 = regexm(names, "info_")
	gen drop2 = regexm(names, "a1_first")
	gen drop3 = regexm(names, "country")
	egen drop = rowtotal(drop*)
	drop if drop > 0
	drop drop*
	gen lb = coef1 + invnormal(.025)*coef2
	gen ub = coef1 + invnormal(.975)*coef2
	gen n = _n
	replace n = n + 1 in 1/5
	replace n = n + 2 in 6/9
	replace n = n + 3 in 10/17
	replace n = n + 4 in 18/19
	replace n = n + 5 in 20/21
	replace n = n + 6 in 22/23
	replace n = n + 7 in 24/25
	tempfile g
	save `g', replace
	clear
	set obs 7
	local m 1 7 12 21 24 27 30
	gen n = .
	local k = 1
	foreach j of local m {
		replace n = `j' in `k'
		local k = `k' + 1
		}
	merge 1:1 n using `g'
	sort n
	drop _m
	gen label = ""
	for any "{bf:Wealth}" "Bottom quintile" "2nd quintile" "3rd quintile" ///
	"4th quintile" "Top quintile" "{bf:Education Level/Prestige}" "Lowest" ///
	"Medium" "Medium-high" "High" ///
	"{bf:Occupation}" "Teacher" "Farmer" "Local legislator" ///
	"Small business" "Accountant" "Surgeon" "Lawyer" "CEO" ///
	"{bf:Partisanship}" "Different" "Same" ///
	"{bf:Gender}" "Different" "Same" ///
	"{bf:Race/Ethnicity}" "Different" "Same" ///
	"{bf:Religion}" "Different" "Same" \ any 1 2 3 4 5 6 7 ///
	8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32: ///
		replace label = "X" in Y
	gen group = (coef1 == .)
	gen length = length(trim(label))
	local maxl = 40
	local pad : di _dup(`maxl') " "
	replace label = trim(label) + substr("`pad'", 1, `maxl' - length) if group == 1
	gen order = _n
	sort order
	local obs = _N
	forval i = 1/`obs' {
		local name = label in `i'
		label define order `i' "`name'", modify
		}
	label values order order
	twoway (rcap ub lb order, horizontal msize(0) lcolor(black)) ///
		(scatter order coef1 if group == 0, mcolor(black) msymbol(o)) ///
		(scatter order coef1 if group == 1, mcolor(none) msymbol(o)), ///
		legend(off) yscale(reverse) ///
		ylabel(#36, valuelab angle(0) labsize(vsmall)) ///
		ytitle("") xline(0, lcolor(black) lwidth(medium) lpattern(dot)) ///
		yline(1 7 12 21 24 27 30, lcolor(gs8) lpattern(solid) lwidth(medium)) ///
		xtitle(`w', size(small)) name(`y', replace)
end

* covariates and other globals
global somecovs i.co_gender i.co_race i.co_relig 
global addcovs a1_first info_treat i.country
global cond if task < 6
global cond2 
global wgt [pw=weight_conjoint]
global wgti [pw=raked_wt]
global tabout age5 educ work_status marital_status religion religiosity racethn ideo5 assets_q
global demos female age51-age55 educ1-educ5 work_status1-work_status5 marital_status1-marital_status5 ///
	religion1-religion4 religiosity1-religiosity5 racethn1-racethn4 urban income2 ///
	assets_q1-assets_q5 ideo51-ideo55 incum
global rt rtitles("Female" \ "Age: 18-24" \ "Age: 25-34" \ "Age: 35-44" ///
		\ "Age: 45-54" \ "Age: 55+" \ "Education: Less than HS" ///
		\ "Education: HS" \ "Education: Some college" ///
		\ "Education: College" \ "Education: Post-graduate" ///
		\ "Work status: Working" \ "Work status: Not working but w/ job" ///
		\ "Work status: Not working but looking" \ "Work status: Retired" ///
		\ "Work status: Other" \ "Marital status: Married" ///
		\ "Marital status: Single" \ "Marital status: Divorced" ///
		\ "Marital status: Separated" ///
		\ "Marital status: Widowed" \ "Religion: Majority" ///
		\ "Religion: 2nd largest" \ "Religion: Other" ///
		\ "Religion: Unaffiliated" \ "Attend religious services: Never" ///
		\ "Attend religious services: Rarely" ///
		\ "Attend religious services: Once per month" ///
		\ "Attend religious services: Once per week" ///
		\ "Attend religious services: More than once per week" ///
		\ "Race/ethnicity: Majority" \ "Race/ethnicity: Largest minority" ///
		\ "Race/ethnicity: 2nd largest minority" \ "Race/ethnicity: Other minority" ///
		\ "Urban" \ "Income: Below median" \ "Income: Above median" ///
		\ "Assets: Bottom quartile" \ "Assets: 2nd quartile" ///
		\ "Assets: 3rd quartile" \ "Assets: Top quartile" ///
		\ "Ideology: Very conservative" \ "Ideology: Somewhat conservative" ///
		\ "Ideology: Moderate" \ "Ideology: Somewhat liberal" ///
		\ "Ideology: Very liberal" \ "Supports incumbent party") ///
	
* binary balance test table
cap prog drop btb
prog def btb
	args x /* x = file name */
	frmttable, clear
	frmttable using "`x'.tex", replace tex fragment ///
		statfont(fs11) hlines(101{0}1) ///
		statmat(bt) substat(0) sdec(0,2,2,2,2,2,2) ///
		$rt ///
		ctitles("","\emph{N}","Mean,","Mean,","Difference","Ranksum","Equivalence","Equivalence" \ ///
			"","","$c1","$c2","","\emph{p}-value","lower \emph{p}-value","upper \emph{p}-value") ///
		title("\caption{$titl\label{tab:$titl_lbl}}\leavevmode") ///
		titlfont(normalsize)
end
	
* conjoint order test table
cap prog drop btco
prog def btco
	args x /* x = file name */
	frmttable using "`x'.tex", replace tex fragment ///
		statfont(fs11) hlines(101{0}1) ///
		statmat(bt) substat(0) sdec(2,2,2,2) ///
		rtitles("Candidate wealth: 2nd quintile" ///
			\ "Candidate wealth: 3rd quintile" \ "Candidate wealth: 4th quintile" ///
			\ "Candidate wealth: Top quintile") ///
		ctitles("","Candidate choice","Clientelism","Effectiveness","Lobby capture") ///
		title("\caption{Balance Tests: $titl\label{tab:$titl_lbl}}\leavevmode") ///
		titlfont(normalsize)
end


*-------- Balance tests: experiment order ---------------*

frmttable, clear
setup_info
tabout
mat bt = J(47,7,.)
loc j = 1
foreach v of varlist $demos {
	qui sum `v'
	mat bt[`j',1] = r(N)
	qui reg `v' a1_first [pw=raked_wt]
	mat bt[`j',2] = _b[_cons]
	mat bt[`j',3] = _b[_cons] + _b[a1_first]
	mat bt[`j',4] = _b[a1_first]
	qui ranksum `v', by(a1_first)
	mat bt[`j',5] = r(p)
	qui tostranksum `v', by(a1_first) eqvt(epsilon) eqvl(3)
	mat bt[`j',6] = r(p1)
	mat bt[`j',7] = r(p2)
	loc j = `j' + 1
}
global c1 "Info. exp. 1st"
global c2 "Info. exp. 2nd"
global titl "Balance Tests: Experiment Order"
global titl_lbl btorder
btb "balance-exp-order"


*-------- Balance tests: Info experiment ---------------*

frmttable, clear
setup_info
tabout
mat bt = J(47,7,.)
loc j = 1
foreach v of varlist $demos {
	qui sum `v'
	mat bt[`j',1] = r(N)
	qui reg `v' info_treat [pw=raked_wt]
	mat bt[`j',2] = _b[_cons]
	mat bt[`j',3] = _b[_cons] + _b[info_treat]
	mat bt[`j',4] = _b[info_treat]
	qui ranksum `v', by(info_treat)
	mat bt[`j',5] = r(p)
	qui tostranksum `v', by(info_treat) eqvt(epsilon) eqvl(3)
	mat bt[`j',6] = r(p1)
	mat bt[`j',7] = r(p2)
	loc j = `j' + 1
}
global c1 "Info. exp.: Control"
global c2 "Info. exp.: Treatment"
global titl "Balance Tests: Information Experiment"
global titl_lbl btinfo
btb "balance-info-exp"
	

*-------- Balance tests: Conjoint experiment ---------------*

frmttable, clear
setup_conjoint
tabout
mat bt = J(47,6,.)
gen c_race = 0
replace c_race = 1 if c_white == 1 & country == 1
replace c_race = 1 if c_mapuche == 0 & country == 3
replace c_race = 1 if c_white == 1 & country == 4
replace c_race = 1 if c_caste == 1 & country == 2
loc j = 1
foreach v of varlist $demos {
	* wealth
	qui reg `v' i.c_wealth $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 2.c_wealth 3.c_wealth 4.c_wealth 5.c_wealth 6.c_wealth ///
		7.c_wealth 8.c_wealth 9.c_wealth
	mat bt[`j',1] = r(p)
	* gender
	qui reg `v' i.c_female $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 1.c_female
	mat bt[`j',2] = r(p)
	* race/ethnicity
	qui reg `v' i.c_race $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 1.c_race
	mat bt[`j',3] = r(p)
	* occpation
	qui reg `v' i.c_occup $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 2.c_occup 3.c_occup 4.c_occup 5.c_occup 6.c_occup ///
		7.c_occup 8.c_occup
	mat bt[`j',4] = r(p)
	* education
	qui reg `v' i.c_edu $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 2.c_edu 3.c_edu 4.c_edu 
	mat bt[`j',5] = r(p)
	* religion
	qui reg `v' i.c_relig $addcovs $cond $cond2 $wgt, cl(caseid)
	qui test 2.c_relig 3.c_relig 4.c_relig 
	mat bt[`j',6] = r(p)
	local j = `j' + 1
}
frmttable using "balance-conjoint-exp.tex", replace tex fragment ///
	statfont(fs11) hlines(11{0}1) ///
	statmat(bt) substat(0) sdec(2,2,2,2) ///
	$rt ///
	ctitles("","Wealth","Gender","Race/Ethnicity","Occupation","Education","Religion") ///
	title("\caption{Balance Tests: Conjoint Experiment\label{tab:btconjoint}}\leavevmode") ///
	titlfont(normalsize)


*-------- Balance tests: Attribute order effects ---------------*

frmttable, clear
setup_conjoint
mat bt = J(4,4,.)
loc j = 1
foreach y in vote vote_buying {
	qui reg `y' i.c_wealth_q##c_wealth_order co_party_order##co_party ///
		c_occup_order##i.c_occup c_edu_order##i.c_edu ///
		co_relig_order##i.co_relig $somecovs ///
		$addcovs $cond $wgt, cl(caseid)
	forval i = 1/4 {
		qui test `i'.c_wealth#3.c_wealth_order `i'.c_wealth#4.c_wealth_order ///
			`i'.c_wealth#5.c_wealth_order `i'.c_wealth#6.c_wealth_order
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
setup_conjoint_us
ren weight weight_conjoint
loc j = 3
foreach y in newleg lobby {
	qui reg `y' i.c_wealth_q##c_wealth_order co_party_order##co_party ///
		c_occup_order##i.c_occup c_edu_order##i.c_edu ///
		co_relig_order##i.co_relig $somecovs ///
		$cond $wgt, cl(caseid)
	forval i = 1/4 {
		qui test `i'.c_wealth#3.c_wealth_order `i'.c_wealth#4.c_wealth_order ///
			`i'.c_wealth#5.c_wealth_order `i'.c_wealth#6.c_wealth_order
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
global titl "Conjoint Experiment Wealth Attribute Order Effects"
global titl_lbl btconjoint_orderw
btco "balance-conjoint-exp-wealth-order"


*-------- Balance tests: Task order effects ---------------*

frmttable, clear
setup_conjoint
mat bt = J(4,4,.)
loc j = 1
foreach y in vote vote_buying {
	qui reg `y' i.task##(i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs) ///
		$addcovs $cond $cond2 $wgt, cl(caseid)
	forval i = 1/4 {
		qui test 2.task#`i'.c_wealth_q 3.task#`i'.c_wealth_q 4.task#`i'.c_wealth_q ///
			5.task#`i'.c_wealth_q
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
setup_conjoint_us
ren weight weight_conjoint
loc j = 3
foreach y in newleg lobby {
	qui reg `y' i.task##(i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs) ///
		$cond $cond2 $wgt, cl(caseid)
	forval i = 1/4 {
		qui test 2.task#`i'.c_wealth_q 3.task#`i'.c_wealth_q
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
global titl "Conjoint Experiment Task Order Effects"
global titl_lbl btconjoint_ordert
btco "balance-conjoint-exp-task-order"


*-------- Balance tests: Profile order effects ---------------*

frmttable, clear
setup_conjoint
mat bt = J(4,4,.)
loc j = 1
foreach y in vote vote_buying {
	qui reg `y' i.profile##(i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs) ///
		$addcovs $cond $cond2 $wgt, cl(caseid)
	forval i = 1/4 {
		qui lincom _b[2.profile#`i'.c_wealth_q]
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
setup_conjoint_us
ren weight weight_conjoint
loc j = 3
foreach y in newleg lobby {
	qui reg `y' i.profile##(i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs) ///
		$cond $cond2 $wgt, cl(caseid)
	forval i = 1/4 {
		qui lincom _b[2.profile#`i'.c_wealth_q]
		mat bt[`i',`j'] = r(p)
	}
	loc j = `j' + 1
}
global titl "Conjoint Experiment Profile Order Effects"
global titl_lbl btconjoint_orderp
btco "balance-conjoint-exp-profile-order"


*-------- Conjoint results by country ---------------*

* (for each individual figure, keep only the applicable country name at the beginning of the loop)
local cs us india chile brazil
forval i = 1/4 {
	loc w : word `i' of `cs'
	setup_conjoint
	global cond2 & country == `i'
	graph_amce vote "Change in probability of choosing a candidate profile"
}
global cond2 


*------ Desired vs. actual legislator wealth in info treatment by country --------*

* (for each individual figure, keep only the applicable country name at the beginning of the loop)
setup_info
local cs us india chile brazil
local p1 28
local p2 20
local p3 20
local p4 15
local l1 -9
local l2 -6.5
local l3 -6.5
local l4 -5
forval i = 1/4 {
	loc w : word `i' of `cs'
	loc t1 "Legislator at the 10th percentile"
	loc t2 "Legislator at the median"
	loc t3 "Legislator at the 90th percentile"
	local j = 1
	foreach x in p10 med p90 {
		sum idist_`x', d
		loc out_max_`x' = r(p99)
		loc out_min_`x' = r(p1)
		hist idist_`x' if idist_`x' < `out_max_`x'' & idist_`x' > `out_min_`x'' ///
			& info_treat == 1 & country == `i', ///
			bin(35) color(black%30) percent ///
			xline(0, lwidth(medthick) lcolor(black) lpattern(solid)) ///
			name(d_`x', replace) note("") title("{bf:`t`j''}" " ", size(small)) ///
			xtitle("") ytitle("") ylabel(, labsize(small)) ///
			xlabel(-9.25 "10,000x" -6.9 "1,000x" -4.6 "100x" -3.2 "25x" -2.3 "10x" -1.7 "5x" -1.1 "3x" -.4 "1.5x" ///
				.4 "1.5x" 1.1 "3x" 1.7 "5x" 2.3 "10x" 3.2 "25x" 4.6 "100x" 6.9 "1,000x" 9.25 "10,000x", labsize(vsmall)) ///
			text(`p`i'' 0 "Desired = actual value {&rarr}", size(vsmall) placement(w)) ///
			text(`l`i'' 0 " {bf:{&larr} Desired < actual}	", size(vsmall) placement(w)) ///
			text(`l`i'' 0 " {bf:	Desired > actual {&rarr}}", size(vsmall) placement(e)) nodraw 
		loc j = `j' + 1
	}
	gr combine d_p10 d_med d_p90, xcommon ycommon col(1) ///
		l1("{it:y}-axis: Percent", size(small)) ///
		b1("{it:x}-axis: Desired v. actual legislator wealth", size(small))
}

	
*----------- Actual legislators' wealth vs. guesses by country -----------------*

* (for each individual figure, keep only the applicable country name at the beginning of the loop)
setup_info
local cs us india chile brazil
local p1 25
local p2 20
local p3 30
local p4 25
local l1 -7
local l2 -6
local l3 -9
local l4 -7
forval i = 1/4 {
	loc w : word `i' of `cs'
	loc t1 "Legislator at the 10th percentile"
	loc t2 "Legislator at the median"
	loc t3 "Legislator at the 90th percentile"
	local j = 1
	foreach x in p10 med p90 {
		sum dist_`x', d
		loc out_max_`x' = r(p99)
		loc out_min_`x' = r(p1)
		hist dist_`x' if dist_`x' < `out_max_`x'' & dist_`x' > `out_min_`x'' ///
			& country == `i', ///
			bin(35) color(black%30) percent ///
			xline(0, lwidth(medthick) lcolor(black) lpattern(solid)) ///
			name(dist_`x', replace) note("") title("{bf:`t`j''}", size(small)) ///
			xtitle("") ytitle("") ylabel(, labsize(small)) ///
			xlabel(-9.25 "10,000x" -6.9 "1,000x" -4.6 "100x" -3.2 "25x" -2.3 "10x" -1.7 "5x" -1.1 "3x" -.4 "1.5x" ///
				.4 "1.5x" 1.1 "3x" 1.7 "5x" 2.3 "10x" 3.2 "25x" 4.6 "100x" 6.9 "1,000x" 9.25 "10,000x", labsize(vsmall)) ///
			text(`p`i'' 0 "{&larr} Guess = actual", size(vsmall) placement(e)) ///
			text(`l`i'' 0 " {bf:{&larr} Guess < actual}		", size(vsmall) placement(w)) ///
			text(`l`i'' 0 " {bf:	Guess > actual {&rarr}}", size(vsmall) placement(e)) nodraw 
		loc j = `j' + 1
	}
	gr combine dist_p10 dist_med dist_p90, xcommon ycommon col(1) ///
		l1("{it:y}-axis: Percent", size(small)) ///
		b1("{it:x}-axis: Guesses v. actual legislator wealth", size(small))
}

	
*----------- Statistical tests for information exepriment -----------------*

frmttable, clear
* For Figure 2: Desired vs. actual legislator wealth in info treatment
setup_info
mat b = J(4,1,.)
mat se = J(4,1,.)
mat pval1 = J(4,2,1)
loc j = 1
foreach x in p10 med p90 ave {
	sum idist_`x', d
	loc out_max_`x' = r(p99)
	loc out_min_`x' = r(p1)
	ttest idist_`x' == 0 if idist_`x' < `out_max_`x'' & idist_`x' > `out_min_`x'' ///
		& info_treat == 1
	mat b[`j',1] = r(mu_1)
	mat se[`j',1] = r(se)
	mat pval1[`j',1] = r(p)
	loc j = `j' + 1
}
mat b1 = b,se
* For Figure 8: Actual legislators' wealth vs. guesses by country
mat pval2 = J(4,2,1)
loc j = 1
foreach x in p10 med p90 ave {
	sum dist_`x', d
	loc out_max_`x' = r(p99)
	loc out_min_`x' = r(p1)
	ttest dist_`x' == 0 if dist_`x' < `out_max_`x'' & dist_`x' > `out_min_`x''
	mat b[`j',1] = r(mu_1)
	mat se[`j',1] = r(se)
	mat pval2[`j',1] = r(p)
	loc j = `j' + 1
}
mat b2 = b,se
mat b = b1,b2
mat pval = pval1,pval2
local cols = colsof(pval)
local rows = rowsof(pval)
mat stars = J(`rows',`cols',0)
forval i = 1/`rows' {
	forval j = 1/`cols' {
		local pval = pval[`i',`j']
	if `pval' <= .10 & `pval' > .05 {
		matrix stars[`i',`j'] = 1
		}
	if `pval' <= .05 & `pval' > .01 {
		matrix stars[`i',`j'] = 2 
		}
	if `pval' <= .01 {
		matrix stars[`i',`j'] = 3
		}
	}
}
frmttable, statmat(b) substat(1) sdec(2) annotate(stars) asymbol(*,**,***) ///
	rtitles("Legislator at the 10th percentile" \ "" \ "Legislator at the median" \ "" \ ///
	"Legislator at the 90th percentile" \ "" \ "Average across scenarios" \ "")
frmttable using "info-ttests.tex", replace tex fragment ///
	statfont(fs11) hlines(101{0}1) ///
	ctitles("","Desired v. actual","Actual v. guessed" \ "","legislators' wealth","legislators' wealth") ///
	title("\caption{\emph{t}-test Results for Figures \ref{fig:pref-optimal} and \ref{fig:pref-guess}\label{tab:info-ttests}}\leavevmode") ///
	titlfont(normalsize)


*----------- Multiple hypothesis testing adjustment -----------------*	
	
setup_conjoint
reg vote i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs ///
	$addcovs $cond $cond2 $wgt, cl(caseid)
	mat coef = e(b)
	mat var = vecdiag(e(V))
	matmap var se, m(sqrt(@))
	mat coef = coef', se'
	scalar R = rowsof(coef)-1
	mat coef = coef[1..R,1..2]
	clear
	svmat coef
	gen names = ""
	local names : rownames coef
	local k = 1
	foreach j of local names {
		replace names = "`j'" in `k'
		local k = `k' + 1
		}
	gen drop1 = regexm(names, "info_")
	gen drop2 = regexm(names, "a1_first")
	gen drop3 = regexm(names, "country")
	egen drop = rowtotal(drop*)
	drop if drop > 0
	drop drop*
	gen lb = coef1 + invnormal(.025)*coef2
	gen ub = coef1 + invnormal(.975)*coef2
	gen n = _n
	replace n = n + 1 in 1/5
	replace n = n + 2 in 6/9
	replace n = n + 3 in 10/17
	replace n = n + 4 in 18/19
	replace n = n + 5 in 20/21
	replace n = n + 6 in 22/23
	replace n = n + 7 in 24/25
	tempfile g
	save `g', replace
	clear
	set obs 7
	local m 1 7 12 21 24 27 30
	gen n = .
	local k = 1
	foreach j of local m {
		replace n = `j' in `k'
		local k = `k' + 1
		}
	merge 1:1 n using `g'
	sort n
	drop _m
	gen label = ""
	for any "{bf:Wealth}" "Bottom quintile" "2nd quintile" "3rd quintile" ///
	"4th quintile" "Top quintile" "{bf:Education Level/Prestige}" "Lowest" ///
	"Medium" "Medium-high" "High" ///
	"{bf:Occupation}" "Teacher" "Farmer" "Local legislator" ///
	"Small business" "Accountant" "Surgeon" "Lawyer" "CEO" ///
	"{bf:Partisanship}" "Different" "Same" ///
	"{bf:Gender}" "Different" "Same" ///
	"{bf:Race/Ethnicity}" "Different" "Same" ///
	"{bf:Religion}" "Different" "Same" \ any 1 2 3 4 5 6 7 ///
	8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32: ///
		replace label = "X" in Y
	gen group = (coef1 == .)
	gen pval = (1-normal(abs(coef1/coef2)))*2
	sort pval
	gen pval_order = _n if pval ~= .
	sum pval_order
	local max = r(max)
	gen bh1 = (pval_order/`max')*0.05
	gen bh2 = (pval < bh1)
	sort n
	gen length = length(trim(label))
	local maxl = 40
	local pad : di _dup(`maxl') " "
	replace label = trim(label) + substr("`pad'", 1, `maxl' - length) if group == 1
	gen order = _n
	sort order
	local obs = _N
	forval i = 1/`obs' {
		local name = label in `i'
		label define order `i' "`name'", modify
		}
	label values order order
twoway (scatter order pval if group == 0 & bh2 == 1, mcolor(black) msymbol(O)) ///
	(scatter order pval if group == 0 & bh2 == 0, mcolor(black) symbol(Oh)) ///
	(scatter order pval if group == 1, mcolor(none)), ///
	legend(off) yscale(reverse) xtitle("{it:p}-value", size(small)) ///
	ylabel(#36, valuelab angle(0) labsize(vsmall)) ///
	ytitle("") yline(1 7 12 21 24 27 30, lcolor(gs8) lpattern(solid) lwidth(medium))
		
		
*----------- Correcting for measurement error -----------------*

setup_conjoint
preserve
drop if country == 1
keep if task == 1 | task == 6
sort caseid task profile
keep caseid task profile vote
keep if profile == 1
bys caseid: gen diff = (vote == vote[_n-1])
sum diff
global me = r(mean)
restore
cap prog drop mes
prog def mes
	args x /* run # */
	reg vote i.c_wealth_q i.c_edu i.c_occup i.co_party $somecovs ///
		$addcovs $cond $cond2 $wgt, cl(caseid)
	mat coef`x' = e(b)
	loc max = colsof(coef`x')
	forval j = 1/`max' {
		nlcom coef`x'[1,`j']/(1-2*$me)
		mat coef`x'[1,`j'] = r(table)[1,1]
	}
	mat coef`x' = coef`x''
end
mes 0
* bootstrapping
set seed 12
forval i = 1/500 {
	di as error "Bootstrap sample `i'"
	preserve
	bsample, cluster(caseid)
	qui mes `i'
	restore
}
clear
forval i = 1/500 {
	svmat coef`i'
}
egen coef2 = rowsd(coef*)
keep coef2
svmat coef0
ren coef0 coef1
order coef1 coef2
loc R = _N
drop in `R'
gen names = ""
local names : rownames coef
local k = 1
foreach j of local names {
	replace names = "`j'" in `k'
	local k = `k' + 1
}
gen drop1 = regexm(names, "info_")
gen drop2 = regexm(names, "a1_first")
gen drop3 = regexm(names, "country")
egen drop = rowtotal(drop*)
drop if drop > 0
drop drop*
gen lb = coef1 + invnormal(.025)*coef2
gen ub = coef1 + invnormal(.975)*coef2
gen n = _n
replace n = n + 1 in 1/5
replace n = n + 2 in 6/9
replace n = n + 3 in 10/17
replace n = n + 4 in 18/19
replace n = n + 5 in 20/21
replace n = n + 6 in 22/23
replace n = n + 7 in 24/25
tempfile g
save `g', replace
clear
set obs 7
local m 1 7 12 21 24 27 30
gen n = .
local k = 1
foreach j of local m {
	replace n = `j' in `k'
	local k = `k' + 1
	}
merge 1:1 n using `g'
sort n
drop _m
gen label = ""
for any "{bf:Wealth}" "Bottom quintile" "2nd quintile" "3rd quintile" ///
	"4th quintile" "Top quintile" "{bf:Education Level/Prestige}" "Lowest" ///
	"Medium" "Medium-high" "High" ///
	"{bf:Occupation}" "Teacher" "Farmer" "Local legislator" ///
	"Small business" "Accountant" "Surgeon" "Lawyer" "CEO" ///
	"{bf:Partisanship}" "Different" "Same" ///
	"{bf:Gender}" "Different" "Same" ///
	"{bf:Race/Ethnicity}" "Different" "Same" ///
	"{bf:Religion}" "Different" "Same" \ any 1 2 3 4 5 6 7 ///
	8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32: ///
		replace label = "X" in Y
gen group = (coef1 == .)
gen length = length(trim(label))
local maxl = 40
local pad : di _dup(`maxl') " "
replace label = trim(label) + substr("`pad'", 1, `maxl' - length) if group == 1
gen order = _n
sort order
local obs = _N
forval i = 1/`obs' {
	local name = label in `i'
	label define order `i' "`name'", modify
	}
label values order order
twoway (rcap ub lb order, horizontal msize(0) lcolor(black)) ///
	(scatter order coef1 if group == 0, mcolor(black) msymbol(o)) ///
	(scatter order coef1 if group == 1, mcolor(none) msymbol(o)), ///
	legend(off) yscale(reverse) ///
	ylabel(#36, valuelab angle(0) labsize(vsmall)) ///
	ytitle("") xline(0, lcolor(black) lwidth(medium) lpattern(dot)) ///
	yline(1 7 12 21 24 27 30, lcolor(gs8) lpattern(solid) lwidth(medium)) 
		
	
*----------- Effects for surprisingly wealthy profiles -----------------*

*** candidate choice
** occupation
setup_conjoint
gen hi_occup = (c_occup == 6 | c_occup == 7 | c_occup == 8)
qui reg vote i.hi_occup##i.c_wealth_q $somecovs i.co_party i.c_edu ///
	$addcovs $cond $wgt, cl(caseid)
margins, at(c_wealth = (0/4) hi_occup = (0 1)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
		title({bf:Candidate Occupation}, nospan) ///
	legend(order(2 "Lower-earning" 4 "Higher-earning") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g1, replace)

** education
setup_conjoint
qui reg vote i.c_edu##i.c_wealth_q $somecovs i.c_occup i.co_party ///
	$addcovs $cond $wgt, cl(caseid)
margins, at(c_wealth = (0/4) c_edu = (1 4)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(green) horizontal) ///
	(scatter n e1 if g == 1, msymbol(T) mcolor(green) msize(medlarge)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(D) mcolor(black)), ///
		title({bf:Candidate Education/Prestige}, nospan) ///
	legend(order(2 "Lower" 4 "Higher") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse off) yscale(range(.75(.25)5.75)) ///
	ylabel(none) ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g2, replace)

** gender
setup_conjoint
qui reg vote i.c_female##i.c_wealth_q i.c_edu i.c_occup i.co_party i.co_race i.co_relig  ///
	$addcovs $cond $wgt, cl(caseid)
margins, at(c_wealth = (0/4) c_female = (1 0)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(magenta) horizontal) ///
	(scatter n e1 if g == 1, msymbol(A) mcolor(magenta) msize(large)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(X) mcolor(black) msize(large)), ///
		title({bf:Candidate Gender}, nospan) ///
	legend(order(2 "Female" 4 "Male") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse alt) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g3, replace)
gr combine g1 g2 g3, ycommon xcommon col(3) ///
	b1("{it:x}-axis: Probability of supporting a candidate profile") ///
	l1("{it:y}-axis: Quintile of candidate's wealth") iscale(*.9)

*** perceptions of effectiveness
setup_conjoint_us
** occupation
gen hi_occup = (c_occup == 6 | c_occup == 7 | c_occup == 8)
qui reg newleg i.hi_occup##i.c_wealth_q $somecovs i.co_party i.c_edu ///
	 if noocc == 0 [pw=weight], cl(caseid)
margins, at(c_wealth = (0/4) hi_occup = (0 1)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
		title({bf:Candidate Occupation}, nospan) ///
	legend(order(2 "Lower-earning" 4 "Higher-earning") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g1, replace)

** education
setup_conjoint_us
qui reg newleg i.c_edu##i.c_wealth_q $somecovs i.c_occup i.co_party ///
	if noocc == 0 [pw=weight], cl(caseid)
margins, at(c_wealth = (0/4) c_edu = (1 4)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(green) horizontal) ///
	(scatter n e1 if g == 1, msymbol(T) mcolor(green) msize(medlarge)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(D) mcolor(black)), ///
		title({bf:Candidate Education/Prestige}, nospan) ///
	legend(order(2 "Lower" 4 "Higher") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse off) yscale(range(.75(.25)5.75)) ///
	ylabel(none) ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g2, replace)

** gender
setup_conjoint_us
qui reg newleg i.c_female##i.c_wealth_q i.c_edu i.c_occup i.co_party i.co_race i.co_relig  ///
	if noocc == 0 [pw=weight], cl(caseid)
margins, at(c_wealth = (0/4) c_female = (1 0)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(magenta) horizontal) ///
	(scatter n e1 if g == 1, msymbol(A) mcolor(magenta) msize(large)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(X) mcolor(black) msize(large)), ///
		title({bf:Candidate Gender}, nospan) ///
	legend(order(2 "Female" 4 "Male") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse alt) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	name(g3, replace)
gr combine g1 g2 g3, ycommon xcommon col(3) ///
	b1("{it:x}-axis: Perceived probability of being an effective legislator") ///
	l1("{it:y}-axis: Quintile of candidate's wealth") iscale(*.9)


*** wealth effects when excluding surprising profiles
setup_conjoint
gen surprise = 0
* low-earning occupation + very high wealth
replace surprise = 1 if c_occup <= 5 & c_wealth >= 8
* low education + very high wealth
replace surprise = 1 if c_edu == 1 & c_wealth >= 8
* female + very high wealth
replace surprise = 1 if c_female == 1 & c_wealth >= 8
drop if surprise == 1
graph_amce vote "Change in probability of choosing a candidate profile"


*--------- Clientelism -------------------*

global cond2 
setup_conjoint
graph_amce vote_buying "Change in perceived probability of candidate giving gifts and money"


*----------- Expectations of clientelism and candidate choice -----------------*

** by income
setup_conjoint
reg vote_buying i.income##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) income = (1 2)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Below-median income respondents" 4 "Above-median income respondents") ///
		ring(1) pos(5) rows(1)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Perceived probability of candidate" "giving gifts and money") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Expectations of Clientelism}") ///
	name(g1, replace)
setup_conjoint
reg vote i.income2##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) income2 = (1 2)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low" 4 "High") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75) alt) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Probability of supporting" "a candidate profile") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Candidate choice}") ///
	name(g2, replace)
grc1leg g1 g2, ycommon


** by wealth
global cond2 
setup_conjoint
reg vote_buying i.assets_q##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) assets_q = (0 4)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low-wealth respondents" 4 "High-wealth respondents") ring(1) pos(5) rows(1)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Perceived probability of candidate" "giving gifts and money") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Expectations of Clientelism}") ///
	name(g1, replace)
setup_conjoint
preserve
collapse assets country $wgt, by(caseid)
drop if assets == .
gen assets_q = .
forval i = 1/4 {
	egen assets_q_`i' = cut(assets) if country == `i', g(5)
	replace assets_q = assets_q_`i' if country == `i'
	drop assets_q_`i'
}
keep caseid assets_q
tempfile a
save `a', replace
restore
merge m:1 caseid using `a'
drop _m
reg vote i.assets_q##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) assets_q = (0 4)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low" 4 "High") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75) alt) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Probability of supporting" "a candidate profile") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Candidate choice}") ///
	name(g2, replace)
grc1leg g1 g2, ycommon


** by education
setup_conjoint
gen higheduc = (educ >= 3) if educ ~= .
reg vote_buying i.higheduc##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) higheduc = (0 1)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low-education respondents" 4 "Higher-education respondents") ///
		ring(1) pos(5) rows(1)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Perceived probability of candidate" "giving gifts and money") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Expectations of Clientelism}") ///
	name(g1, replace)
setup_conjoint
gen higheduc = (educ >= 3) if educ ~= .
reg vote i.higheduc##i.c_wealth_q $somecovs i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) higheduc = (0 1)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low" 4 "High") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75) alt) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Probability of supporting" "a candidate profile") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Candidate choice}") ///
	name(g2, replace)
grc1leg g1 g2, ycommon


** by co-ethnicity
setup_conjoint
reg vote_buying i.co_race##i.c_wealth_q co_gender co_relig i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) co_race = (1 0)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Co-ethnic respondents" 4 "Non-co-ethnic respondents") ///
		ring(1) pos(5) rows(1)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75)) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Perceived probability of candidate" "giving gifts and money") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Expectations of Clientelism}") ///
	name(g1, replace)
setup_conjoint
reg vote i.co_race##i.c_wealth_q co_gender co_relig i.co_party i.c_edu i.c_occup ///
	$addcovs $cond & country ~= 1 $wgt, cl(caseid)
margins, at(c_wealth_q = (0/4) co_race = (1 0)) post
mfxgraph5
twoway (rcap e2 e3 n if g == 1, msize(0) lcolor(orange) horizontal) ///
	(scatter n e1 if g == 1, msymbol(S) mcolor(orange)) ///
	(rcap e2 e3 n if g == 2, msize(0) lcolor(black) horizontal) ///
	(scatter n e1 if g == 2, msymbol(O) mcolor(black)), ///
	legend(order(2 "Low" 4 "High") ring(0) pos(5)) ///
	ytitle("") xtitle("") yscale(reverse) yscale(range(.75(.25)5.75) alt) ///
	ylabel(1.25 `""Lowest" "quintile""' ///
		2.25 "2nd quintile" 3.25 "3rd quintile" 4.25 "4th quintile" ///
		5.25 `""Highest" "quintile""') ///
	yline(1.75 2.75 3.75 4.75, lpattern(solid) lwidth(vthin) lcolor(gs8)) ///
	xtitle("Probability of supporting" "a candidate profile") ///
	ytitle("Quintile of candidate's wealth") title("{bf:Candidate choice}") ///
	name(g2, replace)
grc1leg g1 g2, ycommon


*----------- Desired Legislator Wealth by Accuracy of Respondent Wealth Priors -----------*

setup_info
sum dist_ave, d
global out_max_ave = r(p99)
global out_min_ave = r(p1)
global distif dist_ave < $out_max_ave & dist_ave > $out_min_ave
dist
keep if $distif
twoway (kdensity d_ave if dist_ave_q == 1 ///
		& info_treat == 0 & a1_first == 1, lcolor(orange) lpattern(dash) lwidth(medthick)) ///
	(kdensity d_ave if dist_ave_q == 1 ///
		& info_treat == 1 & a1_first == 1, lcolor(black) lpattern(solid)), ///
	xline(0, lwidth(medthick) lcolor(black) lpattern(solid)) ///
	xlabel(-13 "1,000,000x" -9.25 "10,000x" -6.9 "1,000x" -4.6 "100x" -3.2 "25x" -2.3 "10x" -1.7 "5x" -1.1 "3x" -.4 "1.5x" ///
		.4 "1.5x" 1.1 "3x" 1.7 "5x" 2.3 "10x" 3.2 "25x" 4.6 "100x" 6.9 "1,000x" 9.25 "10,000x" 13 "1,000,000x", labsize(vsmall)) ///
	legend(order(1 "No information on actual legislators' wealth" ///
		2 "Information on actual legislators' wealth") ring(0) pos(6)) ///
	title("{bf:Respondents with inaccurate/low guess}") ///
	text(1 0 "Desired = actual value {&rarr}", size(small) placement(w)) ///
	text(-.35 0 " {bf:{&larr} Desired < actual}	", size(vsmall) placement(w)) ///
	text(-.35 0 " {bf:	Desired > actual {&rarr}}", size(vsmall) placement(e)) ///
	xtitle("") ytitle("") name(g1, replace) nodraw
twoway (kdensity d_ave if dist_ave_q == 4 ///
		& info_treat == 0, lcolor(orange) lpattern(dash) lwidth(medthick)) ///
	(kdensity d_ave if dist_ave_q == 4 ///
		& info_treat == 1, lcolor(black) lpattern(solid)), ///
	xline(0, lwidth(medthick) lcolor(black) lpattern(solid)) ///
	xlabel(-13 "1,000,000x" -9.25 "10,000x" -6.9 "1,000x" -4.6 "100x" -3.2 "25x" -2.3 "10x" -1.7 "5x" -1.1 "3x" -.4 "1.5x" ///
		.4 "1.5x" 1.1 "3x" 1.7 "5x" 2.3 "10x" 3.2 "25x" 4.6 "100x" 6.9 "1,000x" 9.25 "10,000x" 13 "1,000,000x", labsize(vsmall)) ///
	legend(off) ///
	title("{bf:Respondents with accurate guess}") ///
	text(1 0 "Desired = actual value {&rarr}", size(small) placement(w)) ///
	text(-.35 0 " {bf:{&larr} Desired < actual}	", size(vsmall) placement(w)) ///
	text(-.35 0 " {bf:	Desired > actual {&rarr}}", size(vsmall) placement(e)) ///
	xtitle("") ytitle("") name(g2, replace) nodraw
grc1leg g1 g2, ycommon xcommon col(1) ///
	b1("{it:x}-axis: Desired v. actual legislator wealth", size(small)) ///
	l1("{it:y}-axis: Density", size(small))


** end